﻿function translate(word) {
    var glf = document.getElementById("glf");
    glf.contentWindow.gtrans(word);
};

function findAndReplace(searchText, replacement, searchNode) {
    var regex = typeof searchText === 'string' ? new RegExp(searchText, 'g') : searchText,
    childNodes = (searchNode || document.body).childNodes,
    cnLength = childNodes.length,
    excludes = 'html,head,style,title,link,meta,script,object,textarea,iframe';
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1 && (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, replacement, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data)) {
            continue;
        }
        var parent = currentNode.parentNode,
            frag = (function () {
            var html = currentNode.data.replace(regex, replacement),
                wrap = document.createElement('div'),
                frag = document.createDocumentFragment();
            wrap.innerHTML = html;
            while (wrap.firstChild) {
                frag.appendChild(wrap.firstChild);
            }
            return frag;
        })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
};

var seperator = ' \f\n\r\t\v\u00A0\u2028\u2029\u002C\u002E\u003A\u003B\uFF0C\u3002\u3001\uFF08\uFF09\u300C\u300D';

function inject() {
    findAndReplace('[^' + seperator + ']+', function (term) {
        return '<span onmouseover="translate(this)">' + term + '</span>';
    });
};

function init() {
	/*
    var div = document.createElement('div');
    div.id = 'glfdiv';
    div.style.borderWidth = 'thin';
    div.style.borderStyle = 'outset';
    div.style.position = 'fixed';
    div.style.top = '10px';
    div.style.right = '10px';
    div.style.backgroundColor = 'white';
    div.innerHTML = 'loading...';
    document.body.appendChild(div);
    */
    var glf = document.createElement('iframe');
    glf.id = 'glf';
    glf.style.display = 'none';
    document.body.appendChild(glf);
    
    var doc = glf.contentDocument;
    doc.open();
    doc.write('<head><title>Sample html</title><script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("language", "1");function gtrans(word) {if(word.title) return; word.title = "translating..."; var text = word.innerHTML; google.language.detect(text, function(result) { if (!result.error && result.language) { google.language.translate(text, result.language, parent.lang, function(result) {if (result.translation) { word.title = text + ": " + result.translation;}});}});};</script></head><body><div id="branding" style="background-color:white;color:black;padding:0px 1px;text-align:right;">Chrome小字典</div><script>google.language.getBranding("branding");google.setOnLoadCallback(parent.callback);</script></body>');
    doc.close();
    callback = function () {
    	/*
        div.innerHTML = doc.getElementById('branding').outerHTML;
        div.onclick = function () {
            div.style.display = 'none'
        };
        */
    };
    inject();
};
init();